package com.tang.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.tang.auth.application.convert.AuthUserDTOConverter;
import com.tang.auth.domain.entity.AuthUserBO;
import com.tang.auth.domain.service.AuthUserDomainService;
import com.tang.auth.entity.AuthUserDTO;
import com.tang.auth.entity.AuthUserStatusDTO;
import com.tang.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户模块
 *
 * @author Tang
 * @date 2024/2/15 11:07
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     *
     * @param authUserDto 注册信息
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 8:38
     **/
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.register.info:{}", JSON.toJSONString(authUserDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getUserName()), "用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getEmail()), "邮件地址不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDto.getPassword()), "密码不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDto);
            Boolean register = authUserDomainService.register(authUserBO);
            return Result.ok(register);
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.register.error:{}", JSON.toJSONString(authUserDto), e);
            return Result.fail(false);
        }
    }

    /**
     * 用户更新信息
     *
     * @param authUserDto 更新的信息
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 8:38
     **/
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.update.info:{}", JSON.toJSONString(authUserDto));
            }
            Preconditions.checkNotNull(authUserDto.getUserName(), "用户id不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDto);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.update.error:{}", JSON.toJSONString(authUserDto), e);
            return Result.fail(false);
        }
    }

    /**
     * 用户删除信息
     *
     * @param authUserDto
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 9:35
     **/
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.delete.info:{}", JSON.toJSONString(authUserDto));
            }
            Preconditions.checkNotNull(authUserDto.getId(), "用户id不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDto);
            return Result.ok(authUserDomainService.delete(authUserBO));
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.delete.error:{}", JSON.toJSONString(authUserDto), e);
            return Result.fail(false);
        }
    }

    /**
     * 修改用户的状态
     *
     * @param authUserStatusDTO 用户信息
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 9:44
     **/
    @PostMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserStatusDTO authUserStatusDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.changeStatus.info:{}", JSON.toJSONString(authUserStatusDTO));
            }
            Preconditions.checkNotNull(authUserStatusDTO.getId(), "用户id不能为空");
            Preconditions.checkNotNull(authUserStatusDTO.getStatus(), "用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertStatusDTOToBO(authUserStatusDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.changeStatus.error:{}", JSON.toJSONString(authUserStatusDTO), e);
            return Result.fail(false);
        }
    }


    /**
     * 用户登录方法
     *
     * @param validCode 验证码
     * @return com.tang.common.entity.Result<cn.dev33.satoken.stp.SaTokenInfo>
     * @author Tang
     * @date 2024/2/18 13:20
     **/
    @GetMapping("/doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam String validCode) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.doLogin.info:{}", validCode);
            }
            Preconditions.checkArgument(StringUtils.isNoneBlank(validCode), "验证码不能为空");
            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            return Result.ok(saTokenInfo);
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.doLogin.error:{}", validCode, e);
            return Result.fail(e, "登录失败");
        }
    }

    @GetMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }


    /**
     * 获取用户信息
     *
     * @param authUserDTO 查询条件
     * @return ResultM<>
     * @author Tang
     * @date 2024/2/18 14:06
     **/
    @PostMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.getUserInfo.info:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(StringUtils.isNoneBlank(authUserDTO.getUserName()), "用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDTOToBO(authUserDTO);
            AuthUserBO result = authUserDomainService.getUserInfo(authUserBO);
            return Result.ok(AuthUserDTOConverter.INSTANCE.convertBOToDTO(result));
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.getUserInfo.error:{}", JSON.toJSONString(authUserDTO), e);
            return Result.fail(e, "获取信息失败");
        }
    }

    @GetMapping("/logOut")
    public Result<Void> logOut(@RequestParam String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.AuthUserController.logOut.info:{}", userName);
            }
            Preconditions.checkArgument(StringUtils.isNoneBlank(userName), "用户名不能为空");
            authUserDomainService.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            log.error("com.tang.application.controller.AuthUserController.logOut.error:{}", userName, e);
            return Result.fail(e, "登出失败");
        }
    }
}
